package cn.com.shadowless.xinning.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.mengpeng.mphelper.ToastUtils;

import java.util.List;

import cn.com.shadowless.xinning.MyApplication;
import cn.com.shadowless.xinning.R;
import cn.com.shadowless.xinning.adapter.CarSelectAdapter;
import cn.com.shadowless.xinning.callback.RequestCallBack;
import cn.com.shadowless.xinning.model.RequestResult;
import cn.com.shadowless.xinning.model.bill.BillListInfo;
import cn.com.shadowless.xinning.presenter.BillPresenter;
import cn.com.shadowless.xinning.presenter.OrderPresenter;
import cn.com.shadowless.xinning.view.activity.OrderActivity;
import cn.com.shadowless.xinning.view.activity.OrderMoreInfoActivity;
import top.defaults.view.PickerView;

import static cn.com.shadowless.xinning.view.BaseFragment.initLogoutInfo;

/**
 * The type Dialog utils.
 *
 * @author sHadowLess
 */
public class DialogUtils {

    private DialogUtils() {

    }

    /**
     * Show check message dialog.
     *
     * @param activity  the activity
     * @param checkType the check type
     * @param billCode  the bill code
     * @param userName  the user name
     * @param orderId   the order id
     */
    public static void showCheckMessageDialog(Activity activity, String checkType, String billCode, String userName, String orderId) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        AlertDialog dialog = builder.create();
        View dialogView = View.inflate(activity, R.layout.order_check_msg_dialog, null);
        dialog.setView(dialogView);
        dialog.show();

        EditText dialogCheckMsg = dialogView.findViewById(R.id.dialog_check_msg);
        Button dialogCheckCancel = dialogView.findViewById(R.id.dialog_check_cancel);
        Button dialogCheckApply = dialogView.findViewById(R.id.dialog_check_apply);

        dialogCheckCancel.setOnClickListener(v -> dialog.dismiss());

        dialogCheckApply.setOnClickListener(v -> {
            String checkMsg = dialogCheckMsg.getText().toString().trim();
            if (!"".equals(checkMsg)) {
                switch (checkType) {
                    case "审核通过":
                        OrderPresenter
                                .getSingleton()
                                .getCheckPass(billCode, checkMsg, userName, orderId, new RequestCallBack<Object>() {
                                    @Override
                                    public void success(RequestResult<Object> result) {
                                        String remark = result.getRemark();
                                        if (result.isSucceed()) {
                                            ToastUtils.onSuccessShowToast(remark);
                                            initBillListInfo(activity, userName);
                                        } else {
                                            ToastUtils.onErrorShowToast(remark);
                                        }
                                    }

                                    @Override
                                    public void fail(String errorMsg) {
                                        ToastUtils.onErrorShowToast(errorMsg);
                                    }
                                });
                        break;
                    case "逐级驳回":
                        OrderPresenter
                                .getSingleton()
                                .getCheckRejectPre(billCode, checkMsg, userName, orderId, new RequestCallBack<Object>() {
                                    @Override
                                    public void success(RequestResult<Object> result) {
                                        String remark = result.getRemark();
                                        if (result.isSucceed()) {
                                            ToastUtils.onSuccessShowToast(remark);
                                            activity.startActivity(new Intent(activity, OrderActivity.class));
                                        } else {
                                            ToastUtils.onErrorShowToast(remark);
                                        }
                                    }

                                    @Override
                                    public void fail(String errorMsg) {
                                        ToastUtils.onErrorShowToast(errorMsg);
                                    }
                                });
                        break;
                    case "彻底驳回":
                        OrderPresenter
                                .getSingleton()
                                .getCheckRejectApply(billCode, checkMsg, userName, orderId, new RequestCallBack<Object>() {
                                    @Override
                                    public void success(RequestResult<Object> result) {
                                        String remark = result.getRemark();
                                        if (result.isSucceed()) {
                                            ToastUtils.onSuccessShowToast(remark);
                                            initBillListInfo(activity, userName);
                                        } else {
                                            ToastUtils.onErrorShowToast(remark);
                                        }
                                    }

                                    @Override
                                    public void fail(String errorMsg) {
                                        ToastUtils.onErrorShowToast(errorMsg);
                                    }
                                });
                        break;
                    default:
                        break;
                }
                dialog.dismiss();
            } else {
                ToastUtils.onErrorShowToast("请输入审批意见");
            }
        });
    }

    /**
     * The constant item.
     */
    public static String item;

    /**
     * Show car no dialog.
     *
     * @param activity the activity
     * @param list     the list
     * @param billCode the bill code
     * @param userName the user name
     * @param orderId  the order id
     */
    public static void showCarNoDialog(Activity activity, List<Item> list, String billCode, String userName, String orderId) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        AlertDialog dialog = builder.create();
        View dialogView = View.inflate(activity, R.layout.order_check_car_diaolog, null);
        dialog.setView(dialogView);
        dialog.show();

        PickerView pickerView = dialogView.findViewById(R.id.pickerView);
        Button dialogCheckCarCancel = dialogView.findViewById(R.id.dialog_checkCar_cancel);
        Button dialogCheckCarApply = dialogView.findViewById(R.id.dialog_checkCar_apply);


        CarSelectAdapter carSelectAdapter = new CarSelectAdapter(list);
        pickerView.setAdapter(carSelectAdapter);
        pickerView.setOnSelectedItemChangedListener((pickerView1, previousPosition, selectedItemPosition) -> {
            item = pickerView1.getAdapter().getText(selectedItemPosition);
        });

        dialogCheckCarCancel.setOnClickListener(v -> dialog.dismiss());
        dialogCheckCarApply.setOnClickListener(v -> OrderPresenter
                .getSingleton()
                .getCheckPass(billCode, item, userName, orderId, new RequestCallBack<Object>() {
                    @Override
                    public void success(RequestResult<Object> result) {
                        String remark = result.getRemark();
                        if (result.isSucceed()) {
                            ToastUtils.onSuccessShowToast(remark);
                            initBillListInfo(activity, userName);
                        } else {
                            ToastUtils.onErrorShowToast(remark);
                        }
                    }

                    @Override
                    public void fail(String errorMsg) {
                        ToastUtils.onErrorShowToast(errorMsg);
                    }
                }));
    }

    /**
     * Init bill list info.
     *
     * @param userName the user name
     */
    public static void initBillListInfo(Activity context, String userName) {
        if (null == userName) {
            userName = PreferencesUtils.getString(MyApplication.getInstance(), "userName", "-1");
        }
        String finalUserName = userName;
        BillPresenter
                .getSingleton()
                .getBillListInfo(userName, new RequestCallBack<BillListInfo>() {
                    @Override
                    public void success(RequestResult<BillListInfo> result) {
                        String token = result.getLoginToken();
                        String oldToken = PreferencesUtils.getString(MyApplication.getInstance(), "token", "-1");
                        if (token.equals(oldToken)) {
                            if (null != result.getResult() && result.isSucceed()) {
                                Log.e("TAG", "success: " + result.toString());
                                BillListInfo.DataBean dataBean = result.getResult().getData().get(0);
                                String billCode = dataBean.getBillCode();
                                String billName = dataBean.getBillName();
                                String orderId = dataBean.getOrderId();
                                Intent intent = new Intent(context, OrderMoreInfoActivity.class);
                                intent.putExtra("orderId", orderId);
                                intent.putExtra("billCode", billCode);
                                intent.putExtra("billName", billName);
                                intent.putExtra("userName", finalUserName);
                                context.startActivity(intent);
                                context.finish();
                            } else {
                                ToastUtils.onErrorShowToast("暂无订单列表");
                            }
                        } else {
                            ToastUtils.onErrorShowToast("登录态与服务器不一致");
                            initLogoutInfo(context, finalUserName);
                        }
                    }

                    @Override
                    public void fail(String errorMsg) {
                        ToastUtils.onErrorShowToast(errorMsg);
                    }
                });
    }
}
